package org.example.controller;

import org.example.pojo.OrdersPo;
import org.example.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * @author:Cary
 * @date:2023.06.16
 * @version: dev1.8
 * @deion:订单的控制器
 */
@RestController
@RequestMapping("/user/orders")
public class OrdersController {
    @Autowired private OrdersService ordersService;
    /**
     * 查询全部订单
     * @param: void
     * @return String
     */
    @GetMapping("/ordersapi")
    public String findAllOrders(){
        return ordersService.findAllOrdersList();
    }
    /**
     * 根据id删除订单
     * @param: Integer id
     * @return void
     */
    @PostMapping("/orderapi/{id}")
    public void deleteOrders(@PathVariable Integer id){
        ordersService.deleteOrders(id);
    }
    /**
     * 传入年份和月份查询对应日期的订单总计
     * @param: int year
     * @param: int month
     * @return List<OrdersPo>
     */
    @GetMapping("/count")
    public List<OrdersPo> countOrdersByMonth(@RequestParam("year") int year,@RequestParam("month") int month){
        //计算查询时间范围的起始时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month -1,1,0,0,0);
        //计算查询时间范围的结束时间
        Date startDate = calendar.getTime();

        calendar.set(year,month - 1 ,calendar.getActualMaximum(Calendar.DAY_OF_MONTH),23,59,59);
        Date endDate = calendar.getTime();
        //将时间范围作为参数传递
        List<OrdersPo> orders = ordersService.countOrdersByTimeRange(startDate,endDate);
        return orders;
    }

}
